#!/bin/sh
#_________________________________________
red='\e[91m'
green='\e[92m'
yellow='\e[93m'
magenta='\e[95m'
cyan='\e[96m'
none='\e[0m'
_red() { echo -e ${red}$*${none}; }
_green() { echo -e ${green}$*${none}; }
_yellow() { echo -e ${yellow}$*${none}; }
_magenta() { echo -e ${magenta}$*${none}; }
_cyan() { echo -e ${cyan}$*${none}; }
#___________________________________________

# Root
[[ $(id -u) != 0 ]] && echo -e "\n请使用 ${red}root ${none}用户运行\n" && exit 1
#bit
sys_bit=$(uname -m)
case $sys_bit in
  x86_64)
    zabbix_bit="64"
    ;;
  *)
    echo -e "${red}仅支持64位系统。${none}" && exit 1
    ;;
esac
#version
version_test=`cat /etc/redhat-release|sed -r 's/.* ([0-9]+)\..*/\1/'`
if [[ $(command -v yum) ]] && [[ $(command -v systemctl) ||  $(command -v service) ]] && [[ $version_test = 6 || $version_test = 7 ]]; then
  if [[ $(command -v yum) ]]; then
    cmd="yum"
  fi
else
  echo -e "脚本仅支持centos6、7！"
  exit 1
fi
#set ip
username=zabbix
egrep "^$username" /etc/passwd >/dev/null
if [ $? -eq 0 ]; then  
  echo -e "检测到已有zabbix账户，不再新建"
else
  echo "检测到本机无zabbix相关账户，准备新建账号！"
  echo "zabbix:x:496:502:Zabbix Monitoring System:/var/lib/zabbix:/sbin/nologin" >> /etc/passwd
  echo "zabbix:x:500:" >> /etc/group
  echo "新建账户成功！"
fi

self_ip=0.0.0.0
this_package_ip=0.0.0.0
this_zabbix_ip=0.0.0.0

local_package_ip=10.0.10.151
remote_package_ip=10.151.189.77
local_zabbix_ip=10.0.10.127
remote_zabbix_ip=10.151.189.86

self_ip=`ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
echo -e 此机器IP为${green}$self_ip${none}

is_ip_config_vaild=0
while [ $is_ip_config_vaild == 0 ]
do
if [[ $self_ip == "10.0."* ]];then
  echo "猜测此机器是本地机房机器。位于10.0.*.*网段"
  this_zabbix_ip=$local_zabbix_ip
  this_package_ip=$local_package_ip
  is_ip_config_vaild=1
elif [[ $self_ip == "10.194.200."* ]];then
  echo "猜测此机器是云平台机器。位于10.194.200.*网段"
  this_zabbix_ip=$remote_zabbix_ip
  this_package_ip=$remote_package_ip
  is_ip_config_vaild=1
elif [[ $self_ip == "192.168."* ]];then
  echo "猜测此机器是本地机房机器。位于192.168.*.*网段"
  this_zabbix_ip=$local_zabbix_ip
  this_package_ip=$local_package_ip
  is_ip_config_vaild=1
else
  echo "这个ip有点不太对！"
  read -p "请输入本机ip:" self_ip
  echo -e ${green}成功设定此机器IP为$self_ip${none}
fi
done
zabbix5_installer_base_dir=~/zabbix5_install_temp_dir
echo 准备设定zabbix服务器地址为$this_zabbix_ip
mkdir $zabbix5_installer_base_dir
echo 临时安装目录已设置为$zabbix5_installer_base_dir
cd $zabbix5_installer_base_dir
package_name=centos${version_test}
package_tar_name=${package_name}_zabbix5.tar.gz
echo "开始下载安装包..."
wget http://${this_package_ip}/${package_tar_name}
echo -e "${green}下载成功！${none}开始解压安装包。"
tar -zxf ${package_tar_name}
cd ~/zabbix5_install_temp_dir/${package_name}
echo 已经进入安装目录$(pwd)
install_conf=etc/zabbix5/zabbix5_agentd.conf
sed -i 's@Server=0.0.0.0@Server='${this_zabbix_ip}'@g'  $install_conf
sed -i 's@ServerActive=0.0.0.0@ServerActive='${this_zabbix_ip}'@g'  $install_conf
sed -i 's@Hostname=0.0.0.0@Hostname='${self_ip}'@g'  $install_conf
cp -rf ~/zabbix5_install_temp_dir/${package_name}/var/log /var
cp -rf ~/zabbix5_install_temp_dir/${package_name}/usr /
if [[ $version_test = 7 ]]; then
  echo "开始配置防火墙："
  firewall-cmd --permanent --zone=public --add-rich-rule="rule family='ipv4' source address='${this_zabbix_ip}' port protocol='tcp' port='10086' accept"
  firewall-cmd --reload
  echo -e "${green}防火墙配置成功！${none}"
  cp -rf ~/zabbix5_install_temp_dir/${package_name}/var/run /
  cp -rf ~/zabbix5_install_temp_dir/${package_name}/etc/ /
  chmod 755 /usr/sbin/zabbix5_agentd
  chmod 755 /usr/bin/zabbix5_sender
  chmod 755 /usr/lib/systemd/system/zabbix5-agent.service
  chown zabbix:zabbix /var/log/zabbix5
  chown zabbix:zabbix /var/run/zabbix5
  systemctl enable zabbix5-agent.service
  systemctl start  zabbix5-agent.service
  systemctl status zabbix5-agent.service
elif [[ $version_test = 6 ]]; then
  iptables -I INPUT -s ${this_zabbix_ip} -p tcp --dport 10086 -j ACCEPT
  service iptables save
  service iptables restart
  cp -rf ~/zabbix5_install_temp_dir/${package_name}/var/run /var
  cp -rf ~/zabbix5_install_temp_dir/${package_name}/etc/init.d /etc/rc.d/
  cp -rf ~/zabbix5_install_temp_dir/${package_name}/etc/logrotate.d /etc
  cp -rf ~/zabbix5_install_temp_dir/${package_name}/etc/sysconfig /etc
  cp -rf ~/zabbix5_install_temp_dir/${package_name}/etc/zabbix5 /etc
  chmod 755 /usr/sbin/zabbix5_agentd
  chmod 755 /usr/bin/zabbix5_sender
  chmod 755 /etc/init.d/zabbix5-agent
  chown zabbix:zabbix /var/log/zabbix5
  chown zabbix:zabbix /var/run/zabbix5
  chkconfig --add zabbix5-agent
  chkconfig --list |grep zabbix5-agent
  /etc/init.d/zabbix5-agent start
  /etc/init.d/zabbix5-agent status
fi
rm -rf $zabbix5_installer_base_dir
